<template>
  <div class="box app-container">
    <el-row>
      <el-col :span="24">
        <el-card shadow="never" :body-style="{height:'calc(100vh - 230px)'}">
          <el-col :span="24">
            <div style="float: left;">
              <el-form :inline="true" class="demo-form-inline">
                <el-form-item>
                  <el-button type="primary" @click="dialogVisible=true;dialogTitle='添加';resetForm('ruleForm')" icon="el-icon-plus" size="medium">添 加</el-button>
                </el-form-item>
                <el-form-item>
                  <el-button type="danger" @click="deleteItem" icon="el-icon-delete" size="medium">批量删除</el-button>
                </el-form-item>
              </el-form>
            </div>
            <div style="float:right;">
              <el-form :inline="true" class="demo-form-inline" v-model="formInline">
                <el-form-item>
                  <el-input v-model="formInline.username" placeholder="请输入用户名" size="medium"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="searchItem" size="medium">查询</el-button>
                </el-form-item>
              </el-form>
            </div>
          </el-col>
          <el-col :span="24" style="padding-bottom: 20px;">
            <el-table
              :data="tableData.slice((pages.pageNum-1)*pages.pageSize,pages.pageNum*pages.pageSize)"
              border
              @selection-change="handleSelectionChange"
              style="width: 100%;min-height: 480px">
              <el-table-column
                type="selection"
                width="55">
              </el-table-column>
              <el-table-column
                type="index"
                label="序号"
                width="50">
              </el-table-column>
              <el-table-column
                prop="id"
                label="id"
                width="180"
                >
              </el-table-column>
              <el-table-column
                prop="username"
                label="用户名"
                width="100"
                >
              </el-table-column>
              <el-table-column
                prop="email"
                width="180"
                label="邮箱">
              </el-table-column>
              <el-table-column
                label="角色"
                width="150"
              >
                <template slot-scope="scope">
                  <el-tag v-for="(item,index) in scope.row.role" :key="index">{{item}}</el-tag>
                </template>
              </el-table-column>
              <el-table-column
                prop="date"
                label="修改时间">
              </el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    @click="handleEdit(scope.row)">编辑</el-button>
                  <el-button
                    size="mini"
                    type="danger"
                    @click="handleDelete()">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <Pagination :total="total" :pageNum.sync="pages.pageNum"
                        :limit.sync="pages.pageSize"
                        :pageSizes="pageSizes"
            ></Pagination>
          </el-col>
        </el-card>
        <el-dialog
          :title="dialogTitle"
          :visible.sync="dialogVisible"
          width="30%">
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="用户名" prop="name">
              <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="ruleForm.password" type="password"></el-input>
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="ruleForm.email"></el-input>
            </el-form-item>
            <el-form-item label="角色" prop="type">
              <el-checkbox-group v-model="ruleForm.type">
                <el-checkbox label="admin" name="type"></el-checkbox>
                <el-checkbox label="editor" name="type"></el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="submit('ruleForm')">确 定</el-button>
          </span>
        </el-dialog>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import axios from 'axios'
import Pagination from '@/components/Pagination/Pagination'
export default {
  name: 'index',
  components:{Pagination},
  data() {
    return {
      dialogTitle:'',
      ruleForm: {
        name: '',
        email:'',
        password:'',
        type: [],
      },
      rules: {
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        type: [
          { type: 'array', required: true, message: '请选择角色', trigger: 'change' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
      },
      dialogVisible:false,
      formInline: {
        username: ''
      },
      tableData:[],
      loading:false,
      multipleSelection: [],
      dataList:[],
      total:0,
      pages: {
        pageNum: 1,
        pageSize: 10
      },
      pageSizes: [10, 20, 30, 40, 50, 100],
    }
  },
  methods:{
    resetForm(formName) {
      this.$nextTick(() => {
        this.$refs[formName].resetFields()
      })
    },
    handleEdit(row) {
      this.dialogVisible = true
      this.resetForm('ruleForm')
      this.$nextTick(()=>{
        this.dialogTitle = '编辑'
        this.ruleForm.password = row.password
        this.ruleForm.email = row.email
        this.ruleForm.name = row.username
        this.ruleForm.type.push(row.username)
      })
    },
    submit(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$message({
            message: '模拟保存成功',
            type: 'success'
          });
          this.dialogVisible = false
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    deleteItem() {
      if (this.multipleSelection.length <= 0) {
        this.$message.error('未选中任何行')
      } else {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$message({
            type: 'success',
            message: '模拟删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
    },
    searchItem(){
      let list = []
      this.tableData = this.dataList
      for (let i = 0; i < this.tableData.length; i++) {
        if (this.tableData[i].username.indexOf(this.formInline.username) >= 0) {
          list.push(this.tableData[i])
        }
      }
      this.tableData = list
      this.total = this.tableData.length
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleDelete(index, row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '模拟删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    fetchData() {
      //请求接口加载表格数据
      this.loading = true
      axios.get('http://route.showapi.com/100-27').then(res => {
        this.tableData = res.data._data.data.rows
        this.dataList = res.data._data.data.rows
        this.total = res.data._data.data.total
        this.loading = false
      }).catch(err => {
        this.loading = false
      })
    },
  },
  mounted() {
    this.fetchData()
  }
}
</script>

<style scoped lang="scss">
.box{
  margin-bottom: 50px;
  ::v-deep.el-form-item__label {
    font-weight: 400;
  }
  ::v-deep.cell {
    font-size: 14px;
    font-weight: 400;
    text-align: center;
  }
  ::v-deep.cell{
    .el-tag{
      margin-right: 10px;
    }
  }
  ::v-deep .has-gutter{
    tr{
      th{
        background-color: #F5F7FA;
      }
    }
  }
  ::v-deep.el-dialog{
    width: 500px!important;
    height: 376px;
  }
}
</style>
